define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'warehouse/storage/index' + location.search,
                    add_url: 'warehouse/storage/add',
                    edit_url: 'warehouse/storage/edit',
                    del_url: 'warehouse/storage/del',
                    multi_url: 'warehouse/storage/multi',
                    table: 'storage',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Serial number')},
                        {field: 'product_bn', title: __('Product_bn')},
                        {field: 'batch', title: __('Batch')},
                        {field: 'num', title: __('Num')},
                        {field: 'warehouse.name', title: __('Warehouse.name')},
                        {field: 'location.name', title: __('Location.name')},
                        {field: 'products.name', title: __('Products.name')},
                        {field: 'products.unit', title: __('Products.unit')},
                        {field: 'products.access', title: __('Products.Access'), searchList: {"self":__('Self'),"purchase":__('Purchase'),"outsourcing":__('Outsourcing'),"virtual":__('Virtual'),"custorm":__('Custorm')}, formatter: Table.api.formatter.normal},
                        {field: 'products.testfiles', title: __('Products.testfiles')},
                        {field: 'products.custorm_id', title: __('Products.custorm_id')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    ]
                ],
                searchFormVisible: true
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'warehouse/storage/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'warehouse/storage/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'warehouse/storage/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-warehouseid").data("params",function(obj){
                if($("#product").val() && $("#c-batch").val()){
                    return {custom:{product_bn:$("#product").val(),batch:$("#c-batch").val()}};
                }else{
                    if($("#product").val()){
                        return {custom:{product_bn:$("#product").val()}};
                    }else if($("#c-batch").val()){
                        return {custom:{batch:$("#c-batch").val()}};
                    }else{
                        return true;
                    }
                }
            });
            $("#c-locationid").data("params",function(obj){
                if(!$("#c-warehouseid").val()){
                    layer.alert('请先选择仓库');
                }
                if($("#product").val() && $("#c-batch").val()){
                    return {custom:{warehouseid:$("#c-warehouseid").val(),product_bn:$("#product").val(),batch:$("#c-batch").val()}};
                }else{
                    if($("#product").val()){
                        return {custom:{warehouseid:$("#c-warehouseid").val(),product_bn:$("#product").val()}};
                    }else if($("#c-batch").val()){
                        return {custom:{warehouseid:$("#c-warehouseid").val(),batch:$("#c-batch").val()}};
                    }else{
                        return {custom:{warehouseid:$("#c-warehouseid").val()}};
                    }
                }
            });
            $(document).on('click','.btn-ajaxss', function (event) {
                var url = $(this).attr('data-url');
                if(!url) return false;
                var msg = $(this).attr('data-title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var area = [$(window).width() > 800 ? (width?width:'800px') : '95%', $(window).height() > 600 ? (height?height:'600px') : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(value){
                        if(value){
                            var choose_product = document.getElementById("choose_product");
                            choose_product.innerText = value[0].name;
                            var product = document.getElementById("product");
                            product.value = value[0].bn;
                        }else{
                            Layer.alert('请选择一个产品');
                        }
                    }
                };
                Fast.api.open(url,msg,options);
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            if(Config.row){
                var choose_product = document.getElementById("choose_product");
                choose_product.innerText = Config.row.name;
                var product = document.getElementById("product");
                product.value = Config.row.product_bn;
            }else{
                Layer.alert('未知错误，请联系管理员查看');
            }
            $(document).on('click','.btn-ajaxs', function (event) {
                var url = $(this).attr('data-url');
                if(!url) return false;
                var msg = $(this).attr('data-title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                var area = [$(window).width() > 800 ? (width?width:'800px') : '95%', $(window).height() > 600 ? (height?height:'600px') : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(value){
                        if(value){
                            var choose_product = document.getElementById("choose_product");
                            choose_product.innerText = value[0].name;
                            var product = document.getElementById("product");
                            product.value = value[0].bn;
                        }else{
                            Layer.alert('请选择一个产品');
                        }
                    }
                };
                Fast.api.open(url,msg,options);
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});